package com.rtf.framework.web.mvc.rest.dict;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.rtf.framework.common.dto.UtilDTO;
import com.rtf.framework.common.dto.support.DTOCallbackHandler;
import com.rtf.framework.common.util.UtilCollection;
import com.rtf.framework.common.util.UtilMap;
import com.rtf.framework.common.util.UtilTree;
import com.rtf.framework.web.http.ResponseParam;
import com.rtf.framework.web.mvc.rest.curd.AppFunAdaptorController;
import com.rtf.framework.web.mvc.rest.curd.AppTreeConfig;
import org.joda.time.DateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Set;

@ResponseBody
@RequestMapping("/api/sys/dict")
public class SysDictController extends AppFunAdaptorController<SysDict, Long, SysDictService> {

//    @PostMapping("/bycode")
//    public ResponseParam queryList(@RequestBody Map<String, Object> searchParam) {
//        //1.1 获取字典编码
//        String code = UtilMap.getString(searchParam, "code");
//
//        //2.1 执行查询
//        List<SysDict> targetList = this.getTargetService().queryChildDict(code, false);
//
//        List<Map<String, Object>> resultList = UtilDTO.toDTO(targetList, Lists.newArrayList("code", "name"), null, new DTOCallbackHandler() {
//            @Override
//            public void doHandler(Map<String, Object> dtoMap, Class<?> itemClass) {
//                dtoMap.put("id", dtoMap.get("code"));
//                dtoMap.remove("code");
//            }
//        });
//
//        return ResponseParam.success()
//                .datalist(resultList);
//    }

    @Override
    public String[] buildQueryOrder(Map<String, Object> searchParam) {
        return new String[]{"num_asc"};
    }

    @Override
    public Set<String> getUpdateFields(SysDict targetEntity) {
        return Sets.newHashSet("name", "num", "thumbnail","path","remark");
    }

    /**
     * 查询结果的树配置，默认不转换为树
     *
     * @return
     */
    public AppTreeConfig queryListTreeConfig() {
        AppTreeConfig appTreeConfig = new AppTreeConfig();
        appTreeConfig.setEnable(true);
        return appTreeConfig;
    }

    /**
     * 获取允许返回的字段集合
     *
     * @return
     */
    @Override
    public List<String> getQueryFields(String sourceMethod) {
        List<String> entityFields = super.getQueryFields(sourceMethod);
        entityFields.add("children");
        return entityFields;
    }

    /**
     * 执行分页查询
     *
     * @param searchParam
     * @param pageNum
     * @param pageSize
     * @param queryOrder
     * @return
     */
    public Page doQueryByPage(Map<String, Object> searchParam, int pageNum, int pageSize, String[] queryOrder) {
        //对一级分类执行查询
        searchParam.put("parentId", 0);
        Page<SysDict> page = getTargetService().queryByPage(searchParam, pageNum, pageSize, queryOrder);
        //查询一级分类下的所有分类
        List<SysDict> subClassifyLists = getTargetService().querySubList(
                UtilCollection.extractToSet(page.getContent(), "id", Long.class));
        // 构建目标列表
        List<SysDict> targetList = Lists.newArrayList(page.getContent());
        targetList.addAll(subClassifyLists);

        //构建属性列表
        List<Map<String, Object>> targetMapList = UtilDTO.toDTOExcludeFields(targetList, null, new DTOCallbackHandler() {
            @Override
            public void doHandler(Map<String, Object> dtoMap, Class<?> itemClass) {
                if (dtoMap.get("createDatetime") != null) {
                    dtoMap.put("createDatetime", new DateTime(dtoMap.get("createDatetime")).toString("yyyy-MM-dd HH:mm:ss"));
                }
            }
        });
        targetMapList = UtilTree.getTreeData(targetMapList, "id", "parentId", "children", false);
        if (targetMapList == null) {
            targetMapList = Lists.newArrayListWithCapacity(1);
        }

        return new PageImpl(targetMapList, page.getPageable(), page.getTotalElements());
    }
}
